import router from '@/router'
import { PageEnum } from '@/enums/pageEnum'
import axios from 'axios'
import useUserStore from '@/stores/modules/user'
import { ElMessage, ElMessageBox } from 'element-plus'

const config = {
  baseURL: import.meta.env.VITE_BASE_API,
  headers: {
    'content-type': 'application/json'
    // version: configs.version
  },
  timeout: 10 * 1000 // Timeout
}

const service = axios.create(config)

// 事件集
const eventResponse = {
  // 成功
  success: ({ code, message, data }: Response<any>) => {
    // if (code * 1) {
    //   ElMessage({ type: 'success', message: message })
    // }
    return data
  },
  // 失败
  error: ({ code, message }: Response<any>) => {
    if (code * 1) {
      ElMessage({ type: 'error', message: message })
    }
    return Promise.reject(message)
  },
  // 重定向
  redirect: () => {
    router.push('/')
    return Promise.reject()
  },
  // 打开新的页面
  page: ({ data }: Response<any>) => {
    window.location.href = data.url
    return data
  }
}

service.interceptors.request.use(
  (config) => {
    const userStore = useUserStore()
    const token = userStore.token
    if (token) {
      //@ts-ignore
      config.headers.authorization = token
    }
    return config
  },
  (error) => {
    return error
  }
)
// 响应拦截
service.interceptors.response.use(
  (response: any) => {
    // eslint-disable-next-line default-case
    switch (response.data.code) {
      case 200:
        return eventResponse.success(response.data)
      case 1001:
        return eventResponse.error(response.data)
      case 1006:
        return eventResponse.error(response.data)
      case 5001:
        return eventResponse.error(response.data)
      case 5002:
        ElMessageBox.confirm('登录已过期，请重新登录', '系统提示', {
          confirmButtonText: '重新登录',
          type: 'warning'
        }).then(() => {
          router.push(PageEnum.LOGIN)
        })
        return eventResponse.error(response.data)
      case -1:
        return eventResponse.redirect()
      case 2:
        return eventResponse.page(response.data)
    }
  },
  (error) => {
    ElMessage({ type: 'error', message: error.message })
    return Promise.reject(error)
  }
)

interface Response<T> {
  code: number
  data: T
  msg: string
  show: number
  message?: string
}
interface Requset {
  get(url: string, params?: unknown): Promise<any>
  post(url: string, params?: unknown): Promise<any>
  upload(url: string, params?: unknown): Promise<any>
  delete(url: string, params?: unknown): Promise<any>
  put(url: string, params?: unknown): Promise<any>
}

const requset: Requset = {
  get(url, params) {
    return new Promise((resolve, reject) => {
      // NProgress.start()
      service
        .get(url, { params: params })
        .then((res) => {
          // NProgress.done()
          resolve(res)
        })
        .catch((err) => {
          // NProgress.done()
          reject(err)
        })
    })
  },
  post(url, params) {
    return new Promise((resolve, reject) => {
      // NProgress.start()
      service
        .post(url, params)
        .then((res) => {
          // NProgress.done()
          resolve(res)
        })
        .catch((err) => {
          // NProgress.done()
          reject(err)
        })
    })
  },
  upload(url, file) {
    return new Promise((resolve, reject) => {
      // NProgress.start()
      service
        .post(url, file, {
          headers: { 'Content-Type': 'multipart/form-data' }
        })
        .then((res) => {
          // NProgress.done()
          resolve(res)
        })
        .catch((err) => {
          // NProgress.done()
          reject(err)
        })
    })
  },
  delete(url, params) {
    return new Promise((resolve, reject) => {
      // NProgress.start()
      service
        .delete(url, { params: params })
        .then((res) => {
          // NProgress.done()
          resolve(res)
        })
        .catch((err) => {
          // NProgress.done()
          reject(err)
        })
    })
  },
  put(url, params) {
    return new Promise((resolve, reject) => {
      // NProgress.start()
      service
        .put(url, params)
        .then((res) => {
          // NProgress.done()
          resolve(res)
        })
        .catch((err) => {
          // NProgress.done()
          reject(err)
        })
    })
  }
}
export default requset
